﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.MSProject;
using System.IO;
using System.Reflection;

namespace ArchSoft
{
    class MSProjectStore : IStateMonitor
    {

        #region Fields

        const int PREDEFINEDROWS = 2;
        static short SeriesTaskOutlineLevel = 3;
        static short LeafTaskOutlineLevel = 4;
        ApplicationClass projectApp; //static
        Project project;


        private bool initialzied = true;
        private string _folderPath;
        private string _fullPath;

        #endregion

        #region Constructor(s)

        public MSProjectStore() //static
        {
            projectApp = new ApplicationClass();
            projectApp.Visible = false;
            projectApp.DisplayWindowsInTaskbar = false;
        }

        #endregion

        #region IStateMonitor Members

        public string FolderPath
        {
            set 
            {
                _folderPath = value;
            }
        }

        public void NewProject(string projectName)
        {
            _fullPath = _folderPath + Path.DirectorySeparatorChar + projectName + ".mpp";
            Console.WriteLine("New project at " + _fullPath);

            project = projectApp.Projects.Add(Missing.Value, Missing.Value, Missing.Value);
            project.SaveAs(_fullPath, PjFileFormat.pjMPP, Missing.Value, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            // Create default tasks
            Task rootTask = project.Tasks.Add("Tổng tiến độ dự án ",1);
            project.Tasks.Add("Triển khai", 2).OutlineIndent();
            project.Tasks.Add("Kiểm tra", 3);
            project.Tasks.Add("Chỉnh sửa", 4);
            project.Tasks.Add("Phát hành chính thức", 5);            
        }

        public void AddRootNode(string Node, int StartIndex)
        {
            project.Tasks.Add(Node, StartIndex);
        }

        public void OpenProject(string projectName)
        {
            _fullPath = _folderPath + Path.DirectorySeparatorChar + projectName + ".mpp";
            projectApp.Visible = false;
            //Load project file
            projectApp.FileOpen(_fullPath, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, PjPoolOpen.pjPromptPool, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            project = projectApp.ActiveProject;

            initialzied = false;
        }

        public void NewWindow(string Name, int order)
        {
            if (!ShouldPersist(order)) return;
            Task newTask = project.Tasks.Add(Name, ConvertToAbsoluteOrder(order));
            newTask.OutlineLevel = SeriesTaskOutlineLevel;
        }

        public void RenameWindow(string name, int order)
        {
            if (!ShouldPersist(order)) return;

            project.Tasks[ConvertToAbsoluteOrder(order)].Name = name;
        }

        public void DeleteWindow(int startIndex, int num)
        {
            if (!ShouldPersist(startIndex)) return;

            project.Tasks[ConvertToAbsoluteOrder(startIndex)].Delete();
        }

        public void RenameDrawing(int startIndex, int NumOfChild, string newName)
        {
            if (!ShouldPersist(startIndex)) return;

            Task renameTask = FindTaskByCode(CodeNameUtilities.GetCodeByName(newName),startIndex,NumOfChild);
            if (renameTask!=null)
            {
                renameTask.Name = CodeNameUtilities.ToCodeNameWithoutEnxtension(newName);
            }
        }

        public void NewDrawing(int startIndex, int NumOfChild, string newFile)
        {
            if (!ShouldPersist(startIndex)) return;

            Task newTask = project.Tasks.Add(CodeNameUtilities.ToCodeNameWithoutEnxtension(newFile), ConvertToAbsoluteOrder(startIndex,NumOfChild));
            //newTask.Predecessors = newTask2.ID.ToString();
            newTask.OutlineLevel = LeafTaskOutlineLevel;
        }

        public void DeleteDrawing(int startIndex, int NumOfChild, string deletedFile)
        {
            if (!ShouldPersist(startIndex)) return;

            Task deletedTask = FindTaskByCode(CodeNameUtilities.GetCodeByName(deletedFile),startIndex,NumOfChild);
            if (deletedTask!=null)
            {
                deletedTask.Delete();
            }
        }

        public void CloseProject()
        {
            if (project!=null)
            {
                project.SaveAs(_fullPath, PjFileFormat.pjMPP, Missing.Value, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                
            }
          
        }

        public void FinishOpenProject()
        {
            initialzied = true;
        }

        #endregion

        #region Private Methods

        private int ConvertToAbsoluteOrder(int order)
        {
            return PREDEFINEDROWS + order;
        }

        private int ConvertToAbsoluteOrder(int order, int numOfChild)
        {
            return ConvertToAbsoluteOrder(order) + numOfChild;
        }

        private bool ShouldPersist(int order)
        {
            return initialzied && (order >= 0);
        }

        private Task FindTaskByCode(string code, int startIndex, int length)
        {
            int absStartIndex = ConvertToAbsoluteOrder(startIndex);
            for (int i = 1; i <= length + 1; i++)
            {
                if (project.Tasks[absStartIndex+i].Name.StartsWith(code) )
                {
                    return project.Tasks[absStartIndex + i];
                    //break;
                }
            }
            return null;

        }

        #endregion

        public void CloseApplication() //static
        {
            if (projectApp!=null)
            {
                projectApp.Quit(PjSaveType.pjSave);
                
            }
        }

   
    }
}

#region "  TempCode "
/*
 *  public void NewProject(string projectName)
        {
            _fullPath = _folderPath + Path.DirectorySeparatorChar + projectName + ".mpp";
            Console.WriteLine("New project at " + _fullPath);

            // create new project
            
            //projectApp.FileNew(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            //project = projectApp.ActiveProject;
            project = projectApp.Projects.Add(Missing.Value, Missing.Value, Missing.Value);
            project.SaveAs(_fullPath, PjFileFormat.pjMPP, Missing.Value, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            // Create default tasks
            Task rootTask = project.Tasks.Add("Tổng tiến độ dự án ",1);
            project.Tasks.Add("Triển khai", 2).OutlineIndent();
            project.Tasks.Add("Kiểm tra", 3);
            project.Tasks.Add("Chỉnh sửa", 4);
            project.Tasks.Add("Phát hành chính thức", 5);
            
        }
 */
#endregion